package com.example.capsule_chat0322.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.capsule_chat0322.R;
import com.example.capsule_chat0322.bean.RoomBean;
import com.example.capsule_chat0322.interfaces.ChatRoomItemOnClickInterface;

import java.util.List;

/**
 * @Author zhuzhiming
 * @Date 2022/5/1 21:21
 */
public class ChatRoomListAdapter extends RecyclerView.Adapter<ChatRoomListAdapter.ViewHold> implements View.OnClickListener {

    private List<RoomBean> roomList;
    private ChatRoomItemOnClickInterface chatRoomItemOnClickInterface;
    private RecyclerView rvParent;

    public void setChatRoomItemClickListener(ChatRoomItemOnClickInterface chatRoomItemOnClickInterface) {
        this.chatRoomItemOnClickInterface = chatRoomItemOnClickInterface;
    }

    static class ViewHold extends RecyclerView.ViewHolder {

        LinearLayout chatRoomItem;
        TextView chatRoomName;

        public ViewHold(@NonNull View itemView) {
            super(itemView);
            chatRoomItem = itemView.findViewById(R.id.chat_room_item);
            chatRoomName = itemView.findViewById(R.id.chat_room_name);
        }
    }


    public ChatRoomListAdapter(List<RoomBean> roomList) {
        this.roomList = roomList;
    }

    @NonNull
    @Override
    public ViewHold onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        rvParent = (RecyclerView) parent;
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_room_item, parent, false);
        view.setOnClickListener(this);
        return new ChatRoomListAdapter.ViewHold(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHold holder, int position) {

        RoomBean roomBean = roomList.get(position);
        holder.chatRoomItem.setVisibility(View.VISIBLE);
        holder.chatRoomName.setText(roomBean.getRoomName());
    }

    @Override
    public int getItemCount() {
        return roomList.size();
    }


    @Override
    public void onClick(View view) {
        //根据RecyclerView获得当前View的位置
        int position = rvParent.getChildAdapterPosition(view);
        //程序执行到此，会去执行具体实现的onItemClick()方法
        if (chatRoomItemOnClickInterface != null) {
            chatRoomItemOnClickInterface.onItemClick(rvParent, view, position, roomList.get(position).getRoomName());
        }
    }



}
